package cn.demomaster.quicktreeview.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.demomaster.quicktreeview.TreeNode;
import cn.demomaster.quicktreeview.TreeViewAdapter;
import cn.demomaster.quicktreeview.sample.R;

/**
 * Created Squirrel桓
 */
public class QuickTreeRecyclerAdapter extends TreeViewAdapter {


    public QuickTreeRecyclerAdapter(Context context, List<TreeNode> dataList, int defaultExpandLevel,String focusedNodeId) {
        super(context, dataList, defaultExpandLevel,focusedNodeId);
    }

    //    public QuickTreeRecyclerAdapter(RecyclerView mTree, Context context, List<Node> datas, int defaultExpandLevel, int iconExpand, int iconNoExpand) {
//        super(mTree, context, datas, defaultExpandLevel, iconExpand, iconNoExpand);
//    }
//
//    public QuickTreeRecyclerAdapter(RecyclerView mTree, Context context, List<Node> datas, int defaultExpandLevel) {
//        super(mTree, context, datas, defaultExpandLevel);
//    }
//
//    @Override
//    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        return new MyHoder(View.inflate(mContext, R.layout.list_item,null));
//    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.list_item, null);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
        return new MyHoder(view);
    }

    @Override
    public void onBindViewHolder(TreeNode node, RecyclerView.ViewHolder holder, int position) {
        final MyHoder viewHolder = (MyHoder) holder;
        //todo do something
        viewHolder.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //setChecked(node,viewHolder.cb.isChecked());
            }
        });

//        if (node.isChecked()){
//            viewHolder.cb.setChecked(true);
//        }else {
//            viewHolder.cb.setChecked(false);
//        }
//
//        if (node.getIcon() == -1) {
//            viewHolder.icon.setVisibility(View.INVISIBLE);
//        } else {
//            viewHolder.icon.setVisibility(View.VISIBLE);
//            viewHolder.icon.setImageResource(node.getIcon());
//        }
        viewHolder.label.setText(node.getLevel()+"-"+node.getLable());
    }

    class MyHoder extends RecyclerView.ViewHolder {
        public CheckBox cb;
        public TextView label;
        public ImageView icon;
        public MyHoder(View itemView) {
            super(itemView);
            cb = itemView
                    .findViewById(R.id.cb_select_tree);
            label = itemView
                    .findViewById(R.id.id_treenode_label);
            icon = itemView.findViewById(R.id.icon);
        }

    }
}
